package com.sxg.controller;

import com.sxg.query.SxgOrderQuery;
import com.sxg.utils.code.AjaxResult;
import com.sxg.vo.SxgOrderVO;
import com.sxg.vo.req.OrderPayReqVo;
import com.sxg.vo.req.OrderRemoveReqVo;
import com.sxg.vo.req.OrderReqVo;
import com.sxg.vo.req.OrderSearchReqVo;
import lombok.AllArgsConstructor;
import com.sxg.utils.code.Result;
import com.sxg.service.SxgOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

/**
 * 订单表
 *
 * @author 浪漫写尽歌谱 3298751420@qq.com
 * @since 1.0.0 2024-03-07
 */
@RestController
@RequestMapping("sxg/sxg_order")
@AllArgsConstructor
public class SxgOrderController {
    private final SxgOrderService sxgOrderService;

    @GetMapping("page")
    public AjaxResult page(@Valid SxgOrderQuery query) {
        return sxgOrderService.page(query);
    }

    @GetMapping("{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        return sxgOrderService.getOrderInfoById(id);
    }

    @PostMapping
    public AjaxResult save(@RequestBody OrderReqVo vo) {
        return sxgOrderService.createOrderAndLockStock(vo);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sxg:sxg_order:update')")
    public Result<String> update(@RequestBody @Valid SxgOrderVO vo) {
        sxgOrderService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    public AjaxResult delete(@RequestBody OrderRemoveReqVo vo) {
        return sxgOrderService.delete(vo.getId());
    }

    @PostMapping("pay")
    public AjaxResult pay(@RequestBody @Valid OrderPayReqVo vo) {
        return sxgOrderService.pay(vo.getOrderId());
    }

    @DeleteMapping("close")
    public AjaxResult close(@RequestBody SxgOrderVO sxgOrderVO) {
        return sxgOrderService.close(sxgOrderVO.getOrderId());
    }

    @PostMapping("receiptOfGoods")
    public AjaxResult receiptOfGoods(@RequestBody SxgOrderVO sxgOrderVO) {
        return sxgOrderService.receiptOfGoods(sxgOrderVO.getOrderId());
    }

    @GetMapping("search")
    public AjaxResult search(@RequestBody @Valid OrderSearchReqVo searchReqVo) {
        return sxgOrderService.search(searchReqVo.getText());
    }

}